package com.jojo.intermediate.day15_tree;

import com.jojo.elementary.day11_tree.TreeLevelErgodic;
import com.jojo.elementary.entity.TreeNode;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 给定两个整数数组preorder和inorder，
 * 其中preorder是二叉树的先序遍历，inorder是同一棵树的中序遍历，请构造二叉树并返回其根节点。
 *
 * 示例 1:
 * 输入: preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
 * 输出: [3,9,20,null,null,15,7]
 *
 * 示例 2:
 * 输入: preorder = [-1], inorder = [-1]
 * 输出: [-1]
 */
public class BuildBinaryTree {

    private Map<Integer, Integer> indexMap;

    /** myCode(递归) */
    public TreeNode buildTree(int[] preorder, int[] inorder) {
        if (preorder.length == 0 || inorder.length == 0){
            return null;
        }
        return build(preorder, inorder, 0, preorder.length - 1, 0, inorder.length - 1);
    }

    public TreeNode build(int[] preorder, int[] inorder,int preLeft,int preRight,int inLeft,int inRight){
        if (preLeft > preRight){
            return null;
        }
        TreeNode root = new TreeNode(preorder[preLeft]);
        //mid为中序遍历的根节点的索引
        int mid = 0;
        //找到中序遍历的左右子树
        for (int i = inLeft;i <= inRight;i++){
            if (inorder[i] == preorder[preLeft]){
                mid = i;
                break;
            }
        }
        //左子树的长度
        int leftSize = mid - inLeft;
        root.left = build(preorder,inorder,preLeft + 1,preLeft + leftSize,inLeft,mid - 1);
        root.right = build(preorder,inorder, preLeft + leftSize + 1,preRight,mid + 1,inRight);
        return root;
    }


    /** pe.递归 */
    public TreeNode myBuildTree(int[] preorder, int[] inorder, int preorder_left, int preorder_right, int inorder_left, int inorder_right) {
        if (preorder_left > preorder_right) {
            return null;
        }

        // 前序遍历中的第一个节点就是根节点
        int preorder_root = preorder_left;
        // 在中序遍历中定位根节点
        int inorder_root = indexMap.get(preorder[preorder_root]);

        // 先把根节点建立出来
        TreeNode root = new TreeNode(preorder[preorder_root]);
        // 得到左子树中的节点数目
        int size_left_subtree = inorder_root - inorder_left;
        // 递归地构造左子树，并连接到根节点
        // 先序遍历中「从 左边界+1 开始的 size_left_subtree」个元素就对应了中序遍历中「从 左边界 开始到 根节点定位-1」的元素
        root.left = myBuildTree(preorder, inorder, preorder_left + 1, preorder_left + size_left_subtree, inorder_left, inorder_root - 1);
        // 递归地构造右子树，并连接到根节点
        // 先序遍历中「从 左边界+1+左子树节点数目 开始到 右边界」的元素就对应了中序遍历中「从 根节点定位+1 到 右边界」的元素
        root.right = myBuildTree(preorder, inorder, preorder_left + size_left_subtree + 1, preorder_right, inorder_root + 1, inorder_right);
        return root;
    }

    public TreeNode buildTree2(int[] preorder, int[] inorder) {
        int n = preorder.length;
        // 构造哈希映射，帮助我们快速定位根节点
        indexMap = new HashMap<Integer, Integer>();
        for (int i = 0; i < n; i++) {
            indexMap.put(inorder[i], i);
        }
        return myBuildTree(preorder, inorder, 0, n - 1, 0, n - 1);
    }


    public int rootIndex = 0;
    public int index = 0;
    public TreeNode buildTree3(int[] preorder, int[] inorder) {
        return build(preorder,inorder,Integer.MAX_VALUE);
    }

    public TreeNode build(int[] pre,int[] in,int stop){
        if(rootIndex >= pre.length){
            return null;
        }
        if(in[index] == stop){
            index++;
            return null;
        }
        TreeNode root = new TreeNode(pre[rootIndex++]);
        root.left = build(pre,in,root.val);
        root.right = build(pre,in,stop);
        return root;
    }


    public static void main(String[] args) {
        BuildBinaryTree build = new BuildBinaryTree();
        int[] pre = {3,9,20,15,7};
        int[] in = {9,3,15,20,7};
        TreeNode treeNode = build.buildTree3(pre, in);
        TreeLevelErgodic level = new TreeLevelErgodic();
        List<List<Integer>> lists = level.levelOrder(treeNode);
        System.out.println(lists);
    }
}
